﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace NutriLeaf.Web.Core.Notifications
{
    public class Notification : INotifications
    {
        public void InsertNotification(string notificationText, string metaData, int userId, int id, ref Model.NutriLeafDBEntities context)
        {
            Model.Notification notify = new Model.Notification();
            notify.NotificationText = notificationText;
            notify.Metadata = metaData;
            notify.NotificationType.NotificationTypeID = id;
            context.Notifications.AddObject(notify);
            context.SaveChanges();

        }

        public void InsertNotification(Model.Notification notifications, ref Model.NutriLeafDBEntities context)
        {
            context.Notifications.AddObject(notifications);
            context.SaveChanges();
        }



        public List<Model.Notification> GetFriendNotifications(int userId, DateTime day, ref Model.NutriLeafDBEntities context)
        {
            return context.Notifications.Where(p => p.UserID == userId).ToList();
        }

        public List<Model.Notification> GetNotifications(int UserId, DateTime startDay, DateTime endDay, ref Model.NutriLeafDBEntities context)
        {
            return context.Notifications.Where(p => p.UserID == UserId && p.DateTimeStamp >= startDay && p.DateTimeStamp < endDay).ToList();

        }

        public List<Model.Notification> GetFriendNotifications(int userId, DateTime startDay, DateTime endDay, ref Model.NutriLeafDBEntities context)
        {
            List<Model.Notification> notify = new List<Model.Notification>();
            Model.User user = context.Users.Where(p => p.UserID == userId).First();
            Model.Friend[] f = user.Friends.ToArray();
            foreach (Model.Friend fs in f)
            {
                notify.AddRange(context.Notifications.Where(p => p.UserID == userId).ToArray());
            }
            return notify;
        }

        public List<Model.Notification> GetNotifications(int UserId, DateTime day, Model.NotificationType type, ref Model.NutriLeafDBEntities context)
        {
            return context.Notifications.Where(p => p.UserID == UserId && p.DateTimeStamp >= day && p.DateTimeStamp < day && p.DateTimeStamp >= day && p.NotificationType == type).ToList();
        }

        public List<Model.Notification> GetFriendNotifications(int userId, DateTime day, Model.NotificationType type, ref Model.NutriLeafDBEntities context)
        {

            List<Model.Notification> notify = new List<Model.Notification>();
            Model.User user = context.Users.Where(p => p.UserID == userId).First();
            Model.Friend[] f = user.Friends.ToArray();
            foreach (Model.Friend fs in f)
            {
                notify.AddRange(context.Notifications.Where(p => p.UserID == userId && p.DateTimeStamp >= day && p.DateTimeStamp < day).ToArray());
            }
            return notify;
        }

        public List<Model.Notification> GetNotifications(int UserId, DateTime startDay, DateTime endDay, Model.NotificationType type, ref Model.NutriLeafDBEntities context)
        {
            return context.Notifications.Where(p => p.UserID == UserId && p.DateTimeStamp >= startDay && p.DateTimeStamp <= endDay && p.NotificationType == type).ToList();
        }

        public List<Model.Notification> GetFriendNotifications(int userId, DateTime startDay, DateTime endDay, Model.NotificationType type, ref Model.NutriLeafDBEntities context)
        {
            List<Model.Notification> notify = new List<Model.Notification>();
            Model.User user = context.Users.Where(p => p.UserID == userId).First();
            Model.Friend[] f = user.Friends.ToArray();
            foreach (Model.Friend fs in f)
            {
                notify.AddRange(context.Notifications.Where(p => p.UserID == userId && p.DateTimeStamp >= startDay && p.DateTimeStamp < endDay && p.NotificationType == type).ToArray());
            }
            return notify;
        }


        public void RemoveNotification(Model.Notification notification, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.Notifications.Where(p => p.NotificationID == notification.NotificationID).First());
            context.SaveChanges();
        }

        public void RemoveRecentNotification(int UserId, ref Model.NutriLeafDBEntities context)
        {
            context.DeleteObject(context.Notifications.Where(p => p.UserID == UserId).First());
            context.SaveChanges();
        }

        public void UpdateRecentNotification(int userId, string notificationText, string metaData, Model.NotificationType notificationType, ref Model.NutriLeafDBEntities context)
        {
            //requires update.
        }

        public Model.Notification GetRecentNotification(int userId, ref Model.NutriLeafDBEntities context)
        {
            return null;
            //..doubt 
        }

        public void UpdateUserNotification(Model.Notification notification, ref Model.NutriLeafDBEntities context)
        {
            //..requires update.
        }

        public List<Model.NotificationType> GetAllNotificationTypes(ref Model.NutriLeafDBEntities context)
        {
            return context.NotificationTypes.ToList();
        }

        public Model.NotificationType GetNotificationTypeForANotification(int notificationId, ref Model.NutriLeafDBEntities context)
        {
            if (context.NotificationTypes.Where(p => p.NotificationTypeID == notificationId).Count() != 0)
            {
                return context.NotificationTypes.Where(p => p.NotificationTypeID == notificationId).First();
            }
            else
                return null;
        }


        public void InsertNotifications(List<Model.Notification> notificaitons, ref Model.NutriLeafDBEntities context)
        {
            foreach (Model.Notification notify in notificaitons)
            {
                context.Notifications.AddObject(notify);

            }
            context.SaveChanges();
        }



        public List<Model.Notification> GetNotifications(int UserId, DateTime day, ref Model.NutriLeafDBEntities context)
        {
            return context.Notifications.Where(p => p.UserID == UserId).ToList();
        }
    }
}